/**
 * @author hunhunzhang
 * @time 2023/04/08
 * 为了不是每一次都要写一下基本初始化three的环境，简单的统一导出
 */

 import * as THREE from "three";
 import { OrbitControls } from "three/examples/jsm/controls/OrbitControls";
 import { GUI } from "three/examples/jsm/libs/lil-gui.module.min.js";
 import Stats from "three/examples/jsm/libs/stats.module";
 import { CSS2DRenderer } from "three/examples/jsm/renderers/CSS2DRenderer";
 import { RGBELoader } from "three/examples/jsm/loaders/RGBELoader";

 class Tshare {
   constructor(config) {
     this.container = config?.container;
     this.sceneGraph = new THREE.Group();
   }
   initEnv(container) {
    this.container = container;
    const { clientWidth, clientHeight } = container;
    this.scene = new THREE.Scene();
    this.scene.background = new THREE.Color(0x262f45); //6c7d81
    this.scene.add(this.sceneGraph);
    this.camera = new THREE.PerspectiveCamera(
      60,
      clientWidth / clientHeight,
      0.01,
      5000
    );
    this.camera.position.set(0, 0, 5);
    this.camera.lookAt(new THREE.Vector3(0, 0, 0));
    this.renderer = new THREE.WebGLRenderer({ alpha: true, antialias: true });
    this.renderer.setPixelRatio(window.devicePixelRatio);
    this.renderer.setSize(clientWidth, clientHeight);
    container.appendChild(this.renderer.domElement);
    return this;
  }
   initCSS2DRender(container) {
     const { clientWidth, clientHeight } = container;
     this.css2dRenderer = new CSS2DRenderer();
     this.css2dRenderer.setSize(clientWidth, clientHeight);
     this.css2dRenderer.domElement.style.position = "absolute";
     this.css2dRenderer.domElement.style.top = "0px";
     container.appendChild(this.css2dRenderer.domElement);
     return this;
   }
   initOrbitControls() {
     this.controls = new OrbitControls(this.camera, this.renderer.domElement);
     return this;
   }
   addBoxHelper() {
     this.boxHelper = new THREE.BoxHelper(); // 生成包围盒助手
     this.boxHelper.geometry.computeBoundingSphere();
     this.scene.add(this.boxHelper);
     return this;
   }
   loadHdrToScene = (url_default) => {
     let url = url_default ? url_default : "./hdr/default.hdr";
     const pmremGenerator = new THREE.PMREMGenerator(this.renderer); // 使用hdr作为背景色
     pmremGenerator.compileEquirectangularShader();
     new RGBELoader().load(url, (texture) => {
       const envMap = pmremGenerator.fromEquirectangular(texture).texture;
       envMap.isPmremTexture = true;
       pmremGenerator.dispose();
       this.scene.environment = envMap; // 给场景添加环境光效果
     });
     return this;
   };
   getObjectMaterial(object, index) {
     let material = object?.material;
     if (Array.isArray(material) && index !== undefined) {
       material = material[index];
     }
     return material;
   }
   createSphereMesh() {
    let sphere = new THREE.Mesh(
      new THREE.SphereGeometry(0.5, 50, 50),
      new THREE.MeshPhysicalMaterial({
        color: 0xffffff,
        metalness: 0,
        roughness: 0.2,
        clearcoat: 0.5,
      })
    );
    return sphere;
  }
   addStats(){
    this.stats = new Stats();
    this.container.appendChild(this.stats.dom);
    return this;
   }
   addGui(title = "程序控制器") {
    this.gui = new GUI({title});
    return this;
   }
 }
 
 export { Tshare };
 